<script lang="ts" setup>
import { useGlobalDialog } from '@/hooks'
import type { ResultType } from '@/type/type'
import {
  addClassifyApi,
  type ClassifyDTO,
  type ClassifyVO,
  classifyVO2DTO,
  delClassifyApi,
  getClassifyListApi,
  initClassifyDTO,
  updateClassifyApi
} from '@/request/api/classify'
import { DialogEnum } from '@/enum'
import { useSystemStore } from '@/stores/system'

const { globalDialog } = useGlobalDialog()
const systemStore = useSystemStore()

const list = ref<ClassifyVO[]>([])
// 表单
const formRef = ref<any>()
// 多选框 id
const ids = ref<number[]>([])
const isAdd = ref<boolean>(false)

const rules = {
  name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
  priority: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  introduction: [{ required: true, message: '简介不能为空', trigger: 'blur' }]
}
const form = ref<ClassifyDTO>(initClassifyDTO())

/** 查询列表 */
const getList = () => {
  list.value = []
  getClassifyListApi().then((res: ResultType<ClassifyVO[]>) => {
    list.value = res.data
  })
}

// 多选框选中数据
const handleSelectionChange = (selection: ClassifyVO[]) => {
  ids.value = selection.map((item) => item.id)
}

/** 新增、修改按钮操作 */
const handle = (add: boolean, row?: ClassifyVO) => {
  isAdd.value = add
  form.value = initClassifyDTO()
  systemStore.dialog.classify = true
  if (!add && row) {
    form.value = classifyVO2DTO(row)
  }
}

/** 提交按钮 */
const submitForm = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) {
      return
    }
    if (form.value.id != undefined) {
      await globalDialog('确定修改 ID 为"' + form.value.id + '"的类别?').then((res: boolean) => {
        if (res) {
          updateClassifyApi(form.value).then(() => {
            systemStore.dialog.classify = false
            getList()
          })
        }
      })
    } else {
      addClassifyApi(form.value).then(() => {
        systemStore.dialog.classify = false
        getList()
      })
    }
  })
}

/** 删除按钮操作 */
const handleDelete = async (idArr: Array<number>) => {
  await globalDialog('确定删除 ID 为"' + idArr + '"的类别?').then((res: boolean) => {
    if (res) {
      delClassifyApi(idArr).then((res: ResultType<boolean>) => {
        if (res.data) {
          getList()
        }
      })
    }
  })
}

/** 启用停用按钮操作 */
const handleChangeStatus = async (id: number, status: boolean) => {
  const dto: ClassifyDTO = initClassifyDTO()
  dto.id = id
  dto.status = status
  const title = status ? '启用' : '停用'
  await globalDialog('确定' + title + ' ID 为"' + id + '"的类别?').then((res: boolean) => {
    if (res) {
      updateClassifyApi(dto).then((res: ResultType<boolean>) => {
        if (res.data) {
          getList()
        }
      })
    }
  })
}

onMounted(() => {
  getList()
})
</script>

<template>
  <el-container class="pt-5 gap-3" direction="vertical">
    <el-container class="gap-3 pb-2 px-5 flex-wrap">
      <el-button class="!px-2 !m-0" plain type="primary" @click="handle(true)">
        <SvgIcon message="新增" name="add" />
      </el-button>
      <el-button
        :disabled="ids.length === 0"
        class="!px-2 !m-0"
        plain
        type="danger"
        @click="handleDelete(ids)"
      >
        <SvgIcon message="删除" name="delete" />
      </el-button>
    </el-container>

    <el-table :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="35" />
      <el-table-column label="ID" prop="id" width="100" />
      <el-table-column label="类别名" prop="name" width="120" />
      <el-table-column label="类型" prop="type" width="120" />
      <el-table-column label="排序" prop="priority" width="60" />
      <el-table-column label="简介" prop="introduction" />
      <el-table-column label="状态" prop="status" width="70">
        <template #default="scope">
          <el-tag :type="scope.row.status ? 'success' : 'danger'">
            {{ scope.row.status ? '启用' : '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="110">
        <template #default="scope">
          <el-button class="!p-2 !m-1" type="primary" @click="handle(false, scope.row)">
            <SvgIcon name="edit" />
          </el-button>
          <el-button
            v-if="!scope.row.status"
            class="!p-2 !m-1"
            type="success"
            @click="handleChangeStatus(scope.row.id, true)"
          >
            <SvgIcon name="refresh" />
          </el-button>
          <el-button
            v-else
            class="!p-2 !m-1"
            type="danger"
            @click="handleChangeStatus(scope.row.id, false)"
          >
            <SvgIcon name="error" />
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改对话框 -->
    <DialogTemplate :attribute="DialogEnum.CLASSIFY" :title="isAdd ? '新增类别' : '修改类别'">
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item label="类别名" prop="name">
          <el-input v-model="form.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="排序" prop="priority">
          <el-input-number v-model="form.priority" :min="1" />
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <el-container class="justify-between">
        <el-button type="info" @click="systemStore.dialog.classify = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </el-container>
    </DialogTemplate>
  </el-container>
</template>
